import { changePrdCount, deleteCartPrds, getCartList } from "@/api"

const state = ()=>{
    return {
        cartList:[]
    }
}

const getters = {
    // 返回所有选中的商品
    selectProdList: (state)=>{

        return state.cartList.filter(item=>item.choose)
    },
    
    // 是否全部选中
    isAllChoose(state){
        return state.cartList.every(item=>item.choose)
    },


    // 筛选出选中的购物车商品
    filterChoosePrdList(state){
        const li = state.cartList.filter((item)=>{
            return item.choose
        })
        return li
    },

    // 选中的购物车商品总价
    choosePrdPrice(state, getters){
        const lis = getters.filterChoosePrdList
        const totalPrice = lis.reduce((total, item)=>{
            return total + item.goods_num * item.goods.goods_price_min
        }, 0)
        return totalPrice
    },


    // 购物车商品结算个数
    choosePrdCount(state){

        let count = 0
        state.cartList.map((item)=>{
            if(item.choose) {
                count = count + item.goods_num 
            }
        })

        return count
    }

}

const mutations = {

    // 设置购物车数据
    setCartList(state, data){

        // 给每条购物车数据添加选中状态
        const list =  data.map((item)=>{
            // item['choose'] = true
            return item.choose = true
        })

        state.cartList = data
        // console.log(state.cartList)
    },

    // 切换购物车商品的选中状态
    chnagePrdChooseState(state, data){

        state.cartList.map((item)=>{
            if(data.id === item.id) {
                item.choose = !item.choose
            }
        })

        // 判断购物车数据全部选中
    },

    // 全选切换
    cartAllChoose(state, flag) {

        state.cartList.map((item)=>{
            item.choose = flag
        })
    },

    // 更新购物车商品数量
    updateCartPrdCount(state, obj) {

        const {goodsNum, goodsId} = obj

        // 找出商品
        const itemList = state.cartList.filter((item)=>item.goods_id === goodsId)

        // console.log(item)

        itemList[0].goods_num = goodsNum
    }


}

const actions = {

    // 获取购物车数据
    async getCartDataAsync(context){
        try {
                
            const resp = await getCartList()
            // console.log(resp)
            context.commit('setCartList', resp.data.list)
        } catch (error) {
            // console.log(JSON.stringify(error))
        }
    },

    // 购物车商品数量变化
    async asyncChangeCartPrdCount(context, obj){

        try {
            const resp = await changePrdCount(obj)
            // console.log(resp)
        } catch (error) {
            console.log('asyncChangeCartPrdCount----- ' + JSON.stringify(error))
        }
    },

    // 删除购物车数据
    async asyncDeleteCartPrd(context){

        // 获取选中的购物车数据
        const selList = context.getters.filterChoosePrdList


        // 组装删除的购物车数据
        const cartids = selList.map((item)=>{
            return item.id
        })

        const obj = {cartIds : cartids}

        try {

            const resp = await deleteCartPrds(obj)

            // console.log(resp)

            // 更新本地数据
            // 挑选出剩下的数据
            const lastList = context.state.cartList.filter((item)=>{
                return !cartids.includes(item.id)
             })

            //  console.log('11----------------')

            //  console.log(lastList)

            context.commit('setCartList', lastList)


        } catch(err) {
            console.log(err)
        }



    }

}



export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}